//
//  LZUserOperationPresenter.m
//  laziz_Consumer
//
//  Created by xbnzd on 16/11/22.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZUserOperationPresenter.h"
#import "NetworkServiceManager.h"
#import "LZUserModel.h"

extern NSString *customer;

static NSString *kCollectOrCancelCollectUrl = @"/clientcustomerFavorite/update";   //收藏或者取消收藏
static NSString *kZanOrCancelZanUrl = @"/clientcustomerLiked/update";     //赞或者取消赞
static NSString *kSaveRestaurantCommentUrl = @"/clientrestaurantComments/add";  //保存视频评论
static NSString *kSaveVideoCommentUrl = @"/clientvideoComments/add";  //保存视频评论

@implementation LZUserOperationPresenter

#pragma mark -
#pragma mark 收藏or取消收藏 餐厅 or 视频
/**
 收藏或者取消收藏
 
 @param sellerId 商户id
 @param promotionId 优惠id
 @param favoriteType 收藏类型（0餐厅，1视频）
 @param operationType 操作类型（1收藏，0取消收藏）
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)collectOrCancelCollectVideoOrRestaurantWithSellerID:(NSString *)sellerId PromotionID:(NSString *)promotionId FavoriteType:(NSString *)favoriteType OperationType:(NSString *)operationType ResultSuccess:(void (^)(NSInteger , NSInteger))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kCollectOrCancelCollectUrl];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:[LZUserModel Instance].userToken forKey:@"token"];
    if (sellerId) {
        [parameters setObject:sellerId forKey:@"sellerId"];
    }
    if (promotionId) {
        [parameters setObject:promotionId forKey:@"promotionId"];
    }
    if (favoriteType) {
        [parameters setObject:favoriteType forKey:@"favoriteType"];
    }
    if (operationType) {
        [parameters setObject:operationType forKey:@"operationType"];
    }
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parameters success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            NSDictionary *dic = resultData[@"data"];
            if (successBlock) {
                successBlock([dic[@"count"] intValue], [dic[@"type"] intValue]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}


#pragma mark -
#pragma mark 点赞视频或者餐厅
/**
 点赞
 
 @param sellerId 商户id
 @param promotionId 优惠id
 @param type 0 餐厅   1 视频
 @param operationType 1 点赞   0取消点赞
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)zanOrCancelZanVideoOrRestaurantWithSellerID:(NSString *)sellerId PromotionID:(NSString *)promotionId Type:(NSString *)type OperationType:(NSString *)operationType ResultSuccess:(void (^)(NSInteger, NSInteger))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kZanOrCancelZanUrl];
    NSDictionary *dic;
    if (sellerId.length > 0) {
         dic = @{
                  @"sellerId" :sellerId,
                  @"promotionId" : promotionId,
                  @"type" : type,
                  @"operationType" : operationType,
                  @"token" : [LZUserModel Instance].userToken
                  };
    }else{
          dic = @{
                  @"promotionId" : promotionId,
                  @"type" : type,
                  @"operationType" : operationType,
                  @"token" : [LZUserModel Instance].userToken
                  };
    }
    
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        NSDictionary *dic = (NSDictionary *)resultData;
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            NSDictionary *dic = resultData[@"data"];
            if (successBlock) {
                successBlock([dic[@"count"] intValue], [dic[@"type"] intValue]);
            }
        }else{
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 保存餐厅评论
 
 @param parameters 自定义数据字典
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)saveRestaurantCommentWithParameters:(NSMutableDictionary*)parameters ResultSuccess:(void (^)(void))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kSaveRestaurantCommentUrl];
    
    [parameters setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parameters success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock();
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 保存视频评论
 
 @param parameters 自定义数据字典
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)saveVideoCommentWithParameters:(NSMutableDictionary*)parameters ResultSuccess:(void (^)(void))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kSaveVideoCommentUrl];
    
    [parameters setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parameters success:^(NSURLSessionDataTask *task, id resultData) {
        NSDictionary *dic = (NSDictionary *)resultData;
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock();
            }
        }else{
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}


@end
